from mycelery.main import app
from datetime import (datetime, timedelta)
from luffy.settings.contants import ORDER_OUTTIME
from order.models import Order


@app.task(name='check_orders')
def check_order():
    """
    订单超时取消任务
    :return:
    """
    print('定时任务开始执行了！')
    now_time = datetime.now()
    out_time = ORDER_OUTTIME
    # 订单最迟的支付时间 = 当前时间 - 订单可以支付的时间范围
    final_pay_time = now_time - timedelta(seconds=out_time)
    # 查询订单超时仍未支付的订单
    order_list = Order.objects.filter(order_status=0, created_time__lt=final_pay_time)

    for order in order_list:
        order.order_status = 3
        order.save()

# 启动任务
# celery -A mycelery.main worker -l info -P gevent
